<?php 

Class shakeModel extends AppModel
{
	
	public $record = "nv7_record";
	public $join = "nv7_join";
	public $total = "nv7_total";
	
	public function lists($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1 and  p1.wxid=p2.id and p1.score>0 ";
		$order = "order by total desc ,  p1.addtime desc ";
		$result=$this->get_page(" p1.* , (p1.score+p1.add_score) as total,  p2.nickname  ,p2.province ",$this->table($this->record)." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'20',$order);
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		
		$sql="SELECT count(*) FROM ".$this->table($this->record)."  as p1 where p1.score>0 ";
			
		$count=$this->getOne($sql);
		$listPage['count']=$count;
	
		return $listPage;
	}
	
	
	public function all($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1 and  p1.wxid=p2.id and p1.score>0 ";
		$order = "order by total desc ,  p1.addtime desc ";
		$result=$this->get_page(" p1.* , (p1.score+p1.add_score) as total,  p2.nickname  ,p2.province ",$this->table('nv1_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'20',$order);
		//$listPage['pagelink']=$this->getpagelink();
		//$listPage['data']=$result;
	
		$sql="SELECT count(*) FROM ".$this->table('nv1_record')."  as p1 where p1.score>0 ";
		$count=$this->getOne($sql);
		
		$result2 = $this->get_page(" p1.* , (p1.score+p1.add_score) as total,  p2.nickname  ,p2.province ",$this->table('nv2_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'20',$order);
		$sql="SELECT count(*) FROM ".$this->table('nv2_record')."  as p1 where p1.score>0 ";
		$count2=$this->getOne($sql);
		
		$result3 = $this->get_page(" p1.* , (p1.score+p1.add_score) as total,  p2.nickname  ,p2.province ",$this->table('nv3_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'20',$order);
		$sql="SELECT count(*) FROM ".$this->table('nv2_record')."  as p1 where p1.score>0 ";
		$count3=$this->getOne($sql);
		
		$listPage['data']=array_merge($result,$result2,$result3);
		$listPage['count']=$count+$count2+$count3;
	
		return $listPage;
	}
	
	public function list_province()
	{
		
		$sql = "( select p6.*  from ".$this->table($this->record)."  as p5,  ".$this->table('wxusers')." as p6 where  p5.score>0 and  p5.wxid = p6.id )";
		
// 		$result =$this->getAll($sql);
		
		$sql="SELECT count(p1.province_code)  as nums , p2.id FROM ".$sql." as p1 ,  ".$this->table('areas')." as p2 where  p1.province_code =p2.id  group by p1.province_code ";
		
// 		$result =$this->getAll($sql);
		
		$sql = " select  p3.name , p3.id , p4.nums from ".$this->table('areas')." as p3 left join  ( ".$sql." ) as p4 on p3.id = p4.id   where p3.pid = 0 order by p4.nums desc ";
		
		$result =$this->getAll($sql);
		
		
		$sql = "select count(*) as nums  from ".$this->table($this->record)."  as p5,  ".$this->table('wxusers')." as p6 where p5.score>0 and  p6.province_code=0 and p5.wxid = p6.id ";
		
		$other['nums'] =$this->getOne($sql);
		
		$other['name'] = "其他地区";
		
		$result[] = $other;
// 		echo $result[0]['total_nums'];
		
		
		return $result;
	}
	
	
	public function get_total()
	{
		
		
		$sql="SELECT * FROM ".$this->table($this->total) ;

// 		$sql = " select p3.name , p3.id , p4.nums from ".$this->table('areas')." as p3 left join  ( ".$sql." ) as p4 on p3.id = p4.id  where p3.pid = 0 order by p4.nums desc ";
		
		$result =$this->getRow($sql);
	
		$sql="SELECT prize , count(*) as nums FROM ".$this->table($this->record). "   where  prize>0 group by prize " ;
		
// 		$result['joinnums'] =$this->getOne($sql);

		$re2 =$this->getAll($sql);
		
		$sql2="SELECT count(*) as nums FROM ".$this->table($this->record). "   where  prize=0 and score>0" ;
		
		$result['prize00'] =$this->getOne($sql2);
		
		
		
		$total = $result['prize00'];
		
		foreach ($re2 as $k => $v)
		{
			$index = $v['prize'].$v['prize'];
			$result['prize'.$index] = $v['nums'];
			$total +=$v['nums'];
		}
		
		$result['joinnums'] = $total;
		
		return $result;
	}
	
	
	public function list_city($id)
	{
		
		
// 		$sql="SELECT count(p1.city_code)  as nums , p2.id FROM ".$this->table('wxusers')."  as p1 left join    ".$this->table('areas')." as p2 on p1.city_code =p2.id  where p1.province_code = ".$id." group by p1.city_code ";

// 		$sql = " select p3.name , p3.id , p4.nums from ".$this->table('areas')." as p3 left join  ( ".$sql." ) as p4 on p3.id = p4.id  where p3.pid =  ".$id." order by p4.nums desc ";
		
		
// 		$result =$this->getAll($sql);

	
// 		return $result;
		
		

		$sql = "( select p6.*  from ".$this->table($this->record)."  as p5,  ".$this->table('wxusers')." as p6 where p5.prize >0 and p5.wxid = p6.id )";
		
		
		$sql="SELECT count(p1.city_code)  as nums , p2.id FROM ".$sql." as p1 ,     ".$this->table('areas')." as p2   where p1.city_code =p2.id and  p1.province_code =".$id."  group by p1.city_code  ";
		
		$sql = " select p3.name , p3.id , p4.nums from ".$this->table('areas')." as p3 left join  ( ".$sql." ) as p4 on p3.id = p4.id  where p3.pid =  ".$id." order by p4.nums desc ";
		
		
		$result =$this->getAll($sql);
		
		$sql = "select count(*) as nums  from ".$this->table($this->record)."  as p5,  ".$this->table('wxusers')." as p6 where p5.prize >0 and p6.province_code=".$id." and p5.wxid = p6.id and p6.city_code = 0";
		
		$other['nums'] =$this->getOne($sql);
		
		$other['name'] = "其他地区";
		
		$result[] = $other;
		
		
		return $result;
		
	}
	
	
	

	public function get_levy_records($where)
	{
	
		$sql="SELECT p1.*, p2.nickname, (p1.score+p1.add_score) as total  FROM ".$this->table($this->record)." as p1 left join ".$this->table('wxusers')." as p2 on  p1.wxid=p2.id ".$where." order by total desc ,p1.addtime desc   ";
			
		$result=$this->getAll($sql);
	
		if($result)
		{
				
			$i = 1;

				
			foreach ($result as $k => $v)
			{
					

				$result[$k]['order'] = $i;
				$i++;
			}
				
			return $result;
		}
		else
		{
			return false;
		}
	}
}